package birdsong.server;

import java.util.HashMap;
import java.util.LinkedList;

import birdsong.comm.ServerComm;
import birdsong.comm.ServerSideMessage;
import birdsong.server.BirdsongServer;

public class BirdsongServerDummy implements BirdsongServer {

	/**
	 * List that keeps track of clients and followers on the sistem
	 */
	private HashMap<String, LinkedList<String>> clientList;

	//MINE
	private ServerSideMessage message;




	@Override
	public void start(ServerComm serverComm) {
		
		while(true){
			System.out.println("New cicle");
			/**
			 * receive Server Side message from servercomm
			 */
			if(serverComm.hasNextMessage()){
				message= serverComm.getNextMessage();

				////////////////////////////////////////////////////////////////////////////////////
				/**
				 * Message type BIRDSONG_MESSAGE:
				 * sends message to self and followers
				 * 
				 */

				if (message.getType() == ServerSideMessage.Type.BIRDSONG_MESSAGE) {

					serverComm.sendBirdsongMessage(message.getClientNickname(), 
							message.getClientNickname(), 
							"You said: " + message.getPayload());


					for (String s : clientList.get(message.getClientNickname())) {

						serverComm.sendBirdsongMessage(message.getClientNickname(), 
								message.getClientNickname(), 
								message.getClientNickname()+" said: " + message.getPayload());


					}



				}



				/////////////////////////////////////////////////////////////////////////////////
				/**
				 * Message type follow:
				 * adds follower to the hash map
				 * 
				 */
				if (message.getType() == ServerSideMessage.Type.FOLLOW) {

					clientList.get(message.getPayload()).add(message.getClientNickname());
					serverComm.sendNewFollower(message.getPayload(),message.getClientNickname());

				}

				///////////////////////////////////////////////////////////////////////////////////////////

				/**
				 * Message type unfollow:
				 */






				/////////////////////////////////////////////////////////////////////////////////////////////


				/**
				 * Message type CLIENT_CONNECTED:
				 */




				/////////////////////////////////////////////////////////////////////////////////////////////



				/**
				 * Message type CLIENT_DISCONNECTED:
				 */

			}
		}




	}

}